<?php

//Functions to load a file in absolute mode (relatively to the current file)
function & rel($r, &$f) {return file_exists( ( $f = ( dirname($r).'/'.$f ) ) );}
function & relro($r, $f) {return rel($r,$f) ? require_once($f) : null;}

/**
 * Insert a log into the defined log file
 * @param string $p_message a message
 */
function infoLog($p_message){
	$conf = array('mode' => 0600, 'timeFormat' => '%X %x');
	$log  = Log::singleton('file', LOG_FILE, 'iatb', $conf, LOG_INFO);
	$log->log($p_message, LOG_INFO);
	$log->close();
}

/**
 * Return the URL of the current page
 * @return string
 */
function getSelfURL() {
	return getServerURL($_SERVER['REQUEST_URI']);
}

/**
 * Return the URL of the current page
 * @param string an URI (which is to extends the server part of the URL)
 * @return string
 */
function getServerURL($p_uri) {
	$s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
	$protocol = strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
	$port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
	return $protocol."://".$_SERVER['SERVER_NAME'].$port.$p_uri;
}
function strleft($s1, $s2) {
	return substr($s1, 0, strpos($s1, $s2));
}

/**
 * Create a long random key
 * @return string a bas64 encoded key
 */
function getLongKey(){
	return base64_encode(md5(uniqid(rand(), true)). md5(uniqid(rand(), true)));
}

/**
 * Return the current date
 * @return string the current date as String
 */
function getCurrentDate(){
	return date("Y/m/d", time());
}

/**
 * Check the specified date structure.
 * <ul>
 * <li>Y :  the year (example: 2008)</li>
 * <li>m : the month (example: 12)</li>
 * <li>d : the day (example: 01)</li>
 * </ul>
 * @param array an array which contains a date
 * @return boolean True if the date is valid
 */
function isDateValid($p_dateArray){
	return checkdate($p_dateArray["m"], $p_dateArray["d"], $p_dateArray["Y"]);
}

/**
 * An utility method that deal with PDO bug regarding boolean transtyping (what a pity)
 */
function modifiedBindParam(&$p_statement,$p_parameter,$p_value){
	error_log($p_parameter." ".gettype($p_value)." [".$p_value."]");
	if(gettype($p_value)=="boolean" && !$p_value){
		$l_adapted_value=0;
		$p_statement->bindParam($p_parameter, $l_adapted_value);
	}else{
		$p_statement->bindParam($p_parameter, $p_value);
	}
}
?>